package com.ai.zuochengyun.phase01.class03;

/**
 * 获取整棵二叉树的最大距离
 */
public class Code21_GetMaxDistanceBT {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(((int)Math.pow(2, i))+"-->"+Integer.numberOfLeadingZeros((int)(Math.pow(2, i))));
        }
    }

    public static Info process(BinaryTreeNode x) {
        if (x == null) {
            return new Info(0, 0);
        }

        Info leftInfo = process(x.left);
        Info rightInfo = process(x.right);

        int height = Math.max(leftInfo.height, rightInfo.height) + 1;

        // 不经过X
        int p1 = leftInfo.maxDistance;
        int p2 = rightInfo.maxDistance;

        // 经过X
        int p3 = Math.max(leftInfo.maxDistance, rightInfo.maxDistance) + 1;

        int maxDistance = Math.max(Math.max(p1, p2), p3);

        return new Info(maxDistance, height);
    }


    private static class Info {

        public int maxDistance;

        public int height;

        public Info(int maxDistance, int height) {
            this.maxDistance = maxDistance;
            this.height = height;
        }
    }
}
